#-*- coding: utf-8 -*-
from openerp.osv import osv, fields
import time

class ship(osv.Model):
    
    _name="chus.ship"
    
    _columns = {
        'code': fields.char('Code'),
        'customer':fields.many2one('res.partner', 'Customer'),
        'date': fields.datetime('Shipping Date', readonly='True'),
        'ship_from': fields.many2one('res.country', 'Shipping from'),
        'address_ship_to': fields.char('Shipping to address '),
        'state': fields.selection((
                                   ('n','New'),
                                   ('wcr1','Waiting check price'),
                                   ('wcr2', 'Waiting payment'),
                                   ('s','Start shipping'),
                                   ('s1', 'Product in store in US'),
                                   ('s2','Product in store in VN'),
                                   ('d','Done'),
                                   ('close','Cancel'),
                                   ), 
                                  string='State'),
        'rate': fields.float('Rate'), # ti gia
        'ship_line': fields.one2many('chus.ship.line','ship', string='shipping'),
    }
    
    _defaults = {
        'state': 'n',
        'date': lambda self, cr, uid, context={}: context.get('date', time.strftime("%Y-%m-%d %H:%M:%S")),
    }
    
    def set_wcr1(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 'wcr1'}, context=context)

    def set_wcr2(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 'wcr2'}, context=context)

    def set_startShip(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 's'}, context=context)
    def set_s1(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 's1'}, context=context)

    def set_s2(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 's2'}, context=context)

    def set_done(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 'd'}, context=context)
    
    def set_close(self, cr, uid, ids, context=None):
        return self.write(cr, uid, ids, {'state': 'close'}, context=context) 
    
    
    def _get_sequence(self, cr, uid, context=None):
        obj_seq = self.pool.get('ir.sequence')
        args_search = [('code', '=', 'chus.ship')]
        sequence_ids = obj_seq.search(cr, uid, args_search, context=context)

        return obj_seq.next_by_id(cr, uid, sequence_ids[0], context=context)

    def create(self, cr, uid, vals, context=None):
        if context == None:
            context = {}

        vals['code'] = self._get_sequence(cr, uid, context)

        return super(ship, self).create(cr, uid, vals, context=context)